// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import store from "./store"
Vue.config.productionTip = false
// 引入请求框架
import axios from 'axios';
Vue.prototype.$http = axios;
//引入 Element
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

// import VueAreaLinkage from 'vue-area-linkage';
// Vue.use(VueAreaLinkage)

// 全局后台接口api
import api from '@/api/HttpIndex.js' // 导入api接口
Vue.prototype.$api = api; // 将api挂载到vue的原型上
//全局函数
import base from './common/base'//引用

// if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
//   store.dispatch('GetUserInfo').then(res => { // 拉取user_info
//     const roles = res.data.roles // note: roles must be a array! such as: ['editor','develop']
//     store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
//       router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
//       next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
//     })
//   }).catch((err) => {
//     store.dispatch('FedLogOut').then(() => {
//       Message.error(err || 'Verification failed, please login again')
//       next({ path: '/' })
//     })
//   })


  Vue.use(base);//将全局函数当做插件来进行注册
  Vue.use(Vuex);
  router.beforeEach((to, from, next) => {
    var userdata = store.state.userDATA
    if (userdata.token) { // 判断是否有token
      if (to.path === '/login') {  // 判断是否在访问登录页面
        next({ path: '/' });
      } else {
        if (userdata.isroles) { // 判断当前用户是否已拉取完roles信息 
          store.commit("userLoginUpdata",false); 
          store.commit("UserRouterUpdata"); //根据身份渲染路由
          // console.log('main 渲染一次路由')
          next()
        } else {
          next()
        }

      }
    } else {
      // console.log('3')
      next();
    }
  });

  router.afterEach((to, from, next) => {
    window.scrollTo(0, 0)
  });

  new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
  })
